$(function () {

    /*选择器抽取的变量*/
    var pro_datagrid = $("#pro_datagrid");
    var pro_dialog = $("#pro_dialog");
    var pro_form = $("#pro_form");
    /*库存低于10件的商品数量*/
    $.get("/productStock/getLow.do",function (data) {
        $("#lessProduct").html(data);
    })

    /*easyui的数据列表组件初始化*/
    pro_datagrid.datagrid({
        title: "商品管理",
        url: "/product/query.do",
        /*fit: true,*/
       /* width:600,
        height:500,*/
        striped: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        toolbar: "#pro_toolbar",
        columns: [[
            {field: 'name', title: '商品名', width: 100},
            {title:'图片',field:'image',width:60,align:'center',
                formatter:function(value,row,index){return '<img src="'+row.headImg+'" style="width: 60px;height: 60px"/>';}
            },
            {field: 'salePrice', title: '商品售价', width:60},
            {
                field: 'first', title: '一级分类', width: 100,  formatter: function (value) {
                    return value ? value.name : '';
                }
            },
            {
                field: 'second', title: '二级分类', width: 100,  formatter: function (value) {
                    return value ? value.classify : '';
                }
            },
            {field: 'sn', title: '商品编码', width: 60},
            {field: 'remark', title: '备注', width: 60},
        ]]
    })
    //一级分类的编辑框
    $("#firstSelected").combobox({
        onSelect:function (entity) {
            $("#secSelected").combobox("clear");
            $("#secSelected").combobox("reload","/secondary/selectByFirstId.do?firstId="+entity.id);
        }
    });
    //一级分类的高级查询框
    $("#firstId").combobox({
        onSelect:function (entity) {
            $("#secondId").combobox("clear");
            $("#secondId").combobox("reload","/secondary/selectByFirstId.do?firstId="+entity.id);
        }
    });
    /*弹出编辑框的初始化*/
    pro_dialog.dialog({
        width: 400,
        height: 370,
        closed: true,
        buttons: [{
            text: "保存",
            iconCls: "icon-save",
            plain: true,
            handler: function () {
                methodObj["save"]();
            }
        }, {
            text: "取消",
            iconCls: "icon-no",
            plain: true,
            handler: function () {
                methodObj["cancel"]();
            }
        }],
        onClose: function () {
            pro_form.form('clear');
        }
    })


    /*导入弹出框*/
    $("#pro_importData").dialog({
        width: 300,
        height: 115,
        closed: true,
        buttons: [{
            text: "上传",
            iconCls: "icon-save",
            plain: true,
            handler: function () {
                $("#importData_form").submit();
            }
        }, {
            text: "取消",
            iconCls: "icon-no",
            plain: true,
            handler: function () {
                methodObj["importCancel"]();
            }
        }],
        onClose: function () {
            $("input[name='file']").prop("value","");
        }
    })

    /*事件对象*/
    var methodObj = {
        /*新增商品按钮事件*/
        add: function () {
            pro_dialog.dialog('setTitle', "新增商品");
            $("#tr_password").show();
            pro_dialog.dialog('open');
        },

        /*编辑商品按钮事件*/
        edit: function () {
            pro_dialog.dialog('setTitle', "编辑");
            /*c查看是否有选中的商品*/
            var row = pro_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'error');
                return;
            }
            //数据回显
            row["first.id"] = row.first.id;
            row["second.id"] = row.second.id;
            pro_form.form('load', row);
            /*商品回显,根据商品id,查对应的商品*/
            $.get("/product/selectAll.do", {id: row.id}, function (data) {
                $("#pro_combobox").combobox('setValues', data);
            })
            $.get("/product/selectAll.do", {id: row.id}, function (data) {
                $("#pro_combobox").combobox('setValues', data);
            })

            /*打开弹出框*/
            pro_dialog.dialog('open');
        },
        reload: function () {
            window.location.href="/product/view.do";
        },

        /*表单保存事件*/
        save: function () {
            pro_form.form("submit", {
                url: "/product/saveOrUpdate.do",
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", "操作成功", "info", function () {
                            /*pro_dialog.dialog("close");*/
                            window.location.href="/product/view.do";
                        });
                    } else {
                        $.messager.alert("温馨提示", data.message, "error");
                    }
                }
            })
        },

        /*表单取消事件*/
        cancel: function () {
            pro_dialog.dialog('close');
        },

        /*高级查询事件*/
        query: function () {
            var keyword = $("#keyword").textbox('getValue');
            var firstId = $("#firstId").combobox('getValue');
            var secondId = $("#secondId").combobox('getValue');

            pro_datagrid.datagrid('load', {
                keyword: keyword,
                firstId: firstId,
                secondId: secondId
            })
        },
        remove: function () {
            var row = pro_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选中一条数据', 'warning');
                return;
            }
            $.messager.confirm('温馨提示', '您确定要删除吗?', function (y) {
                if (y) {

                    $.get("/product/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '删除成功', "info", function () {
                                window.location.href="/product/view.do";
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'error');
                        }
                    }, 'json')
                }
            })
        },


        /*导出事件*/
        export: function () {
            window.open("/product/exportProduct.do?" + $("#file_form").serialize());
        },

        /*导入事件*/
        importData: function () {
            $("#pro_importData").dialog('open');
        },

        importCancel:function () {
            $("#pro_importData").dialog('close');
        }

    }

    /*统一绑定事件*/
    $("a[data-method]").click(function () {
        var method = $(this).data("method");
        methodObj[method]();
    })

})
